package cn.bingoogolapple.bgabanner.demo.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
import cn.bingoogolapple.bgabanner.BGABanner;
import cn.bingoogolapple.bgabanner.demo.App;
import cn.bingoogolapple.bgabanner.demo.R;
import cn.bingoogolapple.bgabanner.demo.engine.Engine;
import cn.bingoogolapple.bgabanner.demo.model.BannerModel;
import cn.bingoogolapple.bgabanner.transformer.RotatePageTransformer;
import cn.bingoogolapple.bgabanner.transformer.TransitionEffect;
import com.ToxicBakery.viewpager.transforms.AccordionTransformer;
import com.ToxicBakery.viewpager.transforms.BackgroundToForegroundTransformer;
import com.ToxicBakery.viewpager.transforms.CubeInTransformer;
import com.ToxicBakery.viewpager.transforms.CubeOutTransformer;
import com.ToxicBakery.viewpager.transforms.DefaultTransformer;
import com.ToxicBakery.viewpager.transforms.DepthPageTransformer;
import com.ToxicBakery.viewpager.transforms.FlipHorizontalTransformer;
import com.ToxicBakery.viewpager.transforms.FlipVerticalTransformer;
import com.ToxicBakery.viewpager.transforms.ForegroundToBackgroundTransformer;
import com.ToxicBakery.viewpager.transforms.RotateDownTransformer;
import com.ToxicBakery.viewpager.transforms.RotateUpTransformer;
import com.ToxicBakery.viewpager.transforms.StackTransformer;
import com.ToxicBakery.viewpager.transforms.ZoomInTransformer;
import com.ToxicBakery.viewpager.transforms.ZoomOutTranformer;
import com.bumptech.glide.Glide;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MyActivity extends Activity
    implements BGABanner.OnItemClickListener, BGABanner.Adapter {
  private BGABanner mAccordionBanner;

  private Engine mEngine;

  @Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mEngine = App.getInstance().getEngine();

    initView();
    setListener();
    loadData();
  }

  private void initView() {
    mAccordionBanner = (BGABanner) findViewById(R.id.banner_main_accordion);
  }

  private void setListener() {
    //mDefaultBanner.setOnItemClickListener(this);
    //mCubeBanner.setOnItemClickListener(this);
  }

  private void loadData() {
    loadData(mAccordionBanner, 3);
  }

  private void loadData(final BGABanner banner, int count) {
    mEngine.fetchItemsWithItemCount(count).enqueue(new Callback<BannerModel>() {
      @Override public void onResponse(Call<BannerModel> call, Response<BannerModel> response) {
        BannerModel bannerModel = response.body();

        banner.setAdapter(MyActivity.this);
        banner.setData(bannerModel.imgs, bannerModel.tips);
      }

      @Override public void onFailure(Call<BannerModel> call, Throwable t) {
        Toast.makeText(App.getInstance(), "网络数据加载失败", Toast.LENGTH_SHORT).show();
      }
    });
  }

  @Override public void onBannerItemClick(BGABanner banner, View view, Object model, int position) {
    Toast.makeText(App.getInstance(), "点击了第" + (position + 1) + "页", Toast.LENGTH_SHORT).show();
  }

  @Override public void fillBannerItem(BGABanner banner, View view, Object model, int position) {
    Glide.with(MyActivity.this)
        .load(model)
        .placeholder(R.drawable.holder)
        .error(R.drawable.holder)
        .into((ImageView) view);
  }

  public void onClick(View v) {
    switch (v.getId()) {
      case R.id.tv_main_select_page_one:
        mAccordionBanner.getViewPager().setPageTransformer(true, new DefaultTransformer());
        break;
      case R.id.tv_main_select_page_two:
        mAccordionBanner.getViewPager().setPageTransformer(true, new AccordionTransformer());
        break;
      case R.id.tv_main_select_page_three:
        mAccordionBanner.getViewPager()
            .setPageTransformer(true, new BackgroundToForegroundTransformer());
        break;
      case R.id.tv_main_select_page_four:
        mAccordionBanner.getViewPager().setPageTransformer(true, new CubeInTransformer());
        break;
      case R.id.tv_main_select_page_five:
        mAccordionBanner.getViewPager().setPageTransformer(true, new CubeOutTransformer());
        break;
      case R.id.tv_main_get_item_count:
        mAccordionBanner.getViewPager().setPageTransformer(true, new DepthPageTransformer());
        break;
      case R.id.tv_main_get_current_item:
        mAccordionBanner.getViewPager().setPageTransformer(true, new FlipHorizontalTransformer());
        break;
      case R.id.tv_main_load_one_item:
        mAccordionBanner.getViewPager().setPageTransformer(true, new FlipVerticalTransformer());
        break;
      case R.id.tv_main_load_two_item:
        mAccordionBanner.getViewPager().setPageTransformer(true, new ForegroundToBackgroundTransformer());
        break;
      case R.id.tv_main_load_three_item:
        mAccordionBanner.getViewPager().setPageTransformer(true, new RotateDownTransformer());
        break;
      case R.id.tv_main_load_five_item:
        mAccordionBanner.getViewPager().setPageTransformer(true, new RotateUpTransformer());
        break;
      case R.id.tv_main_cube:
        mAccordionBanner.getViewPager().setPageTransformer(true, new StackTransformer());
        break;
      case R.id.tv_main_depth:
        mAccordionBanner.getViewPager().setPageTransformer(true, new ZoomInTransformer());
        break;
      case R.id.tv_main_flip:
        mAccordionBanner.getViewPager().setPageTransformer(true, new ZoomOutTranformer());
        break;
      //case R.id.tv_main_rotate:
      //  mDefaultBanner.setTransitionEffect(TransitionEffect.Rotate);
      //  break;
      //case R.id.tv_main_alpha:
      //  mDefaultBanner.setTransitionEffect(TransitionEffect.Alpha);
      //  break;
      case R.id.tv_main_listview_demo:
        startActivity(new Intent(this, ListViewDemoActivity.class));
        break;
      default:
        break;
    }
  }
}
